package com.eduprocess.viewmodel;

import java.util.HashMap;
import java.util.List;

import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

public class JsonResult {

	private boolean result = true;

	private Object obj;

	private HashMap<String, Object> objects = new HashMap<String, Object>();

	private HashMap<String, String> errors = new HashMap<String, String>();

	public boolean isResult() {
		return result;
	}

	public void setResult(boolean result) {
		this.result = result;
	}

	public HashMap<String, String> getErrors() {
		return errors;
	}

	public void setErrors(HashMap<String, String> errors) {
		this.errors = errors;
	}

	public void addError(String key, String error) {
		this.errors.put(key, error);
	}

	public void addError(BindingResult bindingResult) {
		List<FieldError> fieldErrors = bindingResult.getFieldErrors();
		for (FieldError error : fieldErrors) {
			addError(error.getField(), error.getDefaultMessage());
		}
	}

	public Object getObj() {
		return obj;
	}

	public void setObj(Object obj) {
		this.obj = obj;
	}

	public boolean hasErrors() {
		return errors.size() > 0;
	}

	public HashMap<String, Object> getObjects() {
		return objects;
	}

	public void setObjects(HashMap<String, Object> objects) {
		this.objects = objects;
	}

	public void addObject(String key, Object object) {
		objects.put(key, object);
	}

}
